package com.ithiem.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.dubbo.api.UserApi;
import com.itheima.model.domain.User;
import com.ithiem.dubbo.mappers.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

import java.util.List;


@DubboService
public class UserApiImpl implements UserApi {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByMobile(String mobile) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("mobile", mobile);
        return userMapper.selectOne(qw);
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public User findByHuanxinId(String huanxinId) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getHxUser, huanxinId);
        return userMapper.selectOne(lqw);
    }
    /*
     * 查询今日新增用户
     * */
    @Override
    public List<User> findByCreated(Date date) {

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getCreated,date);
        return userMapper.selectList(lqw);
    }

    //    查询活跃了n天及以上的用户总数
    @Override
    public Integer finActivityForDays(Integer days) {
        /*LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getActiveDays,days).or().gt(User::getActiveDays,days);
        return userMapper.selectList(lqw);*/
        return userMapper.findActiveDaysCounts(days);
    }
    //查询用户总人数
    @Override
    public Integer findAllCounts() {

        return userMapper.findAllCounts();
    }
    /**
     * 后台用户分页显示所有用户信息
     * @param ids
     * @return
     */
    @Override
    public Map<Long, User> findByIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new HashMap<>();
        }
        List<User> users = userMapper.selectBatchIds(ids);
        Map<Long, User> map = CollUtil.fieldValueMap(users, "id");
        return map;
    }

}
